<html>
<head><meta charset="utf-8"><title>mutability of captured path · t-compiler/wg-rfc-2229 · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/index.html">t-compiler/wg-rfc-2229</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html">mutability of captured path</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218066523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218066523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218066523">(Nov 27 2020 at 08:28)</a>:</h4>
<p>So if I enable this feature by default then iterator fails to build. </p>
<p>Log: <a href="http://csclub.uwaterloo.ca/~a52arora/rust-builds/log_773e5336-8959-4d16-816a-038b63a0a88c">http://csclub.uwaterloo.ca/~a52arora/rust-builds/log_773e5336-8959-4d16-816a-038b63a0a88c</a></p>



<a name="218066732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218066732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218066732">(Nov 27 2020 at 08:31)</a>:</h4>
<p>I'm wondering if there is something that kind of obvious to you that we might have messed up</p>



<a name="218066799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218066799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218066799">(Nov 27 2020 at 08:32)</a>:</h4>
<p>Our code is here: <a href="https://github.com/sexxi-goose/rust/pull/33">https://github.com/sexxi-goose/rust/pull/33</a></p>



<a name="218066975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218066975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218066975">(Nov 27 2020 at 08:34)</a>:</h4>
<p>The theme is fairly common, move closure and captuing a variable of type <code>&amp;mut T</code></p>



<a name="218067002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218067002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218067002">(Nov 27 2020 at 08:35)</a>:</h4>
<p>My attempts to recreate a minimum  source code didn't work</p>



<a name="218114849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218114849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218114849">(Nov 27 2020 at 17:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span></p>



<a name="218116425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218116425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218116425">(Nov 27 2020 at 17:54)</a>:</h4>
<p>So I was being dumb and didn't have the feature gate <span aria-label="face palm" class="emoji emoji-1f926" role="img" title="face palm">:face_palm:</span>‍♂️ <br>
This fails:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(capture_disjoint_fields)]</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">t</span><span class="p">(</span><span class="n">ref_s</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">*</span><span class="n">ref_s</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>

<span class="w">    </span><span class="n">c</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ref_s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">s</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="n">t</span><span class="p">(</span><span class="n">ref_s</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="218116438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218116438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218116438">(Nov 27 2020 at 17:54)</a>:</h4>
<div class="codehilite"><pre><span></span><code>error[E0594]: cannot assign to `ref_s`, as it is not declared as mutable
  --&gt; move_closure.rs:10:9
   |
7  | fn t&lt;&#39;a&gt;(ref_s: &amp;&#39;a mut i32) {
   |          ----- help: consider changing this to be mutable: `mut ref_s`
...
10 |         *ref_s += 10; //some_num(ref_s);
   |         ^^^^^^^^^^^^ cannot assign
</code></pre></div>



<a name="218152696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218152696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218152696">(Nov 28 2020 at 07:28)</a>:</h4>
<p>So this can be traced when we log here: <a href="https://github.com/sexxi-goose/rust/blob/mir_min_cap_writeback/compiler/rustc_typeck/src/check/upvar.rs#L240-L243">https://github.com/sexxi-goose/rust/blob/mir_min_cap_writeback/compiler/rustc_typeck/src/check/upvar.rs#L240-L243</a></p>
<p>Okay so the problem is when the feature is enabled what gets captured is <code>Place(hir_id_ref_s, base_ty: &amp;mut i32, projections: [(Deref, i32)]</code>, i.e. the type of the capture is <code>i32</code>.</p>
<p>When we don't have the <code>move</code> there then we get a <code>UniqueImmBorrow</code> (instead of a <code>ByValue</code>) which then converts it into <code>mut i32</code> when recording the type. </p>
<p>When the feature isn't enabled then the root variable is captured we get a <code>&amp;mut i32</code> as it should be</p>



<a name="218155025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218155025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218155025">(Nov 28 2020 at 08:47)</a>:</h4>
<hr>
<p>Maybe we just drop all Deref that are not followed by any Field Projections. Because we can't really ger more disjoint after that</p>



<a name="218155075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218155075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218155075">(Nov 28 2020 at 08:48)</a>:</h4>
<p>And since we are keeping an ancestor it technically reduces the amt of work we do later to find a matching capture</p>



<a name="218187044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218187044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218187044">(Nov 28 2020 at 23:56)</a>:</h4>
<p>So this doesn't quite solve the problem</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(capture_disjoint_fields)]</span><span class="w"></span>
<span class="cp">#![feature(rustc_attrs)]</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Point</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">y</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="mi">10</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">mp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">p</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="c1">// #[rustc_capture_analysis]</span>
<span class="w">    </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">mp</span><span class="p">.</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>

<span class="w">    </span><span class="n">c</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Essentially when <code>mp.x</code> is captured we need it to be marked mutable because <code>mp</code> starts off a mutable reference.</p>



<a name="218188362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218188362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218188362">(Nov 29 2020 at 00:37)</a>:</h4>
<p>I guess this is a more interesting example because the mutability can be introduced  from any part in the path and not just the base</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(capture_disjoint_fields)]</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">t1</span><span class="p">.</span><span class="mf">0.0</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>

<span class="w">    </span><span class="n">c</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="218353052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353052">(Nov 30 2020 at 22:07)</a>:</h4>
<p>I'm a bit confused what the problem is here, <span class="user-mention" data-user-id="281950">@Aman Arora</span></p>



<a name="218353071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353071">(Nov 30 2020 at 22:08)</a>:</h4>
<p>oh hmm</p>



<a name="218353120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353120">(Nov 30 2020 at 22:08)</a>:</h4>
<p>well no I'm still a bit confused :)</p>



<a name="218353141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353141">(Nov 30 2020 at 22:08)</a>:</h4>
<p>we certainly expect that <code>t</code> would be modified in place by the above example, right?</p>



<a name="218353161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353161">(Nov 30 2020 at 22:08)</a>:</h4>
<p>yes</p>



<a name="218353188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353188">(Nov 30 2020 at 22:08)</a>:</h4>
<p>So the way I had currently (incorrectly) implemented  mutability for precise captures is if the root variable being captured is mutable</p>



<a name="218353403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353403">(Nov 30 2020 at 22:10)</a>:</h4>
<p>and the reason it ends up working out today is that, at some point when we look at the path within the closure we see the &amp;mut type.  That is <code>t1</code>  is immut</p>
<p>However since we capture <code>t1.0.0</code> which has type <code>i32</code> and not <code>mut i32</code>, we loose the information about it being mutable</p>



<a name="218353441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353441">(Nov 30 2020 at 22:10)</a>:</h4>
<p>I see</p>



<a name="218353481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353481">(Nov 30 2020 at 22:11)</a>:</h4>
<p>well I sort of see</p>



<a name="218353499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353499">(Nov 30 2020 at 22:11)</a>:</h4>
<p>is this a bug you fixed already or not yet :)</p>



<a name="218353527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353527">(Nov 30 2020 at 22:11)</a>:</h4>
<p>Not yet, still trying to figure it out a bit</p>



<a name="218353653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353653">(Nov 30 2020 at 22:11)</a>:</h4>
<p>Might need to store more information in mir::Body or something</p>



<a name="218353826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353826">(Nov 30 2020 at 22:12)</a>:</h4>
<p>I'm assuming this bits needs to get more information. <a href="https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir/src/borrow_check/mod.rs#L157-L181">https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir/src/borrow_check/mod.rs#L157-L181</a></p>



<a name="218353994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218353994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218353994">(Nov 30 2020 at 22:14)</a>:</h4>
<p>Because the way we do it for local variables is stored that info in Mir::Body::local_decls and we can see a use of that here: <a href="https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir/src/borrow_check/mod.rs#L1211-L1215">https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir/src/borrow_check/mod.rs#L1211-L1215</a></p>



<a name="218354467"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354467" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354467">(Nov 30 2020 at 22:18)</a>:</h4>
<p>uh</p>



<a name="218354497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354497">(Nov 30 2020 at 22:18)</a>:</h4>
<p>I wouldn't <em>expect</em> changes in <em>borrow check</em> really</p>



<a name="218354536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354536">(Nov 30 2020 at 22:19)</a>:</h4>
<p>I guess my expectation is that the upvar would ultimately be an <code>&amp;mut</code></p>



<a name="218354550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354550">(Nov 30 2020 at 22:19)</a>:</h4>
<p>I asked around on t-compiler/help over the weekend and was pointed to <a href="https://github.com/rust-lang/rust/blob/c4fe25d8613a32dcd71e5258c22ff5685f12d4c3/compiler/rustc_mir/src/borrow_check/mod.rs#L2156-L2306">https://github.com/rust-lang/rust/blob/c4fe25d8613a32dcd71e5258c22ff5685f12d4c3/compiler/rustc_mir/src/borrow_check/mod.rs#L2156-L2306</a></p>
<p>But I think by the time we get here, we have lost some valuable information</p>



<a name="218354598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354598">(Nov 30 2020 at 22:19)</a>:</h4>
<p>so e.g. if you have <code>|| *x += 1</code></p>



<a name="218354623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354623">(Nov 30 2020 at 22:19)</a>:</h4>
<p>then I would expect us to capture <code>*x</code> by mutable ref</p>



<a name="218354686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354686">(Nov 30 2020 at 22:20)</a>:</h4>
<p>so if e.g. <code>mut x: Box&lt;i32&gt;</code>, then we would have an upvar of type <code>&amp;mut i32</code></p>



<a name="218354687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354687">(Nov 30 2020 at 22:20)</a>:</h4>
<p>It does but if we have a move closure then it won't work</p>



<a name="218354720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354720">(Nov 30 2020 at 22:20)</a>:</h4>
<p>becuase we capture <code>ByValue, [x, projection: (Deref, i32)]</code></p>



<a name="218354776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354776">(Nov 30 2020 at 22:21)</a>:</h4>
<p>is it a <code>FnOnce</code> or <code>FnMut</code> closure...</p>



<a name="218354826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354826">(Nov 30 2020 at 22:21)</a>:</h4>
<p>if <code>FnMut</code>, then I expect that we would reach the upvar via <code>(*self).upvar</code></p>



<a name="218354843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354843">(Nov 30 2020 at 22:21)</a>:</h4>
<p>where <code>self: &amp;mut ClosureType</code>, so the mutability comes from there</p>



<a name="218354857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354857">(Nov 30 2020 at 22:21)</a>:</h4>
<p>Yes</p>



<a name="218354869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354869">(Nov 30 2020 at 22:21)</a>:</h4>
<p>if <code>FnOnce</code> then I guess we need a <code>mut self</code>, but I would expect the same problem today</p>



<a name="218354891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354891">(Nov 30 2020 at 22:22)</a>:</h4>
<p>so presumably we handle it somehow</p>



<a name="218354923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354923">(Nov 30 2020 at 22:22)</a>:</h4>
<p>e.g. by having <code>mut self</code></p>



<a name="218354937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218354937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218354937">(Nov 30 2020 at 22:22)</a>:</h4>
<p>e.g., <code>move || x += 1</code> where <code>x: i32</code></p>



<a name="218355435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218355435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218355435">(Nov 30 2020 at 22:26)</a>:</h4>
<p>I think that will work but I think the problem might be when we deal with references. I don't know when we decide if we need a <code>mut self</code> vs a <code>self</code> for closure, but we start to loose information about mutability in capture analysis. </p>
<p>So for the example below:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(capture_disjoint_fields)]</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">t1</span><span class="p">.</span><span class="mf">0.0</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>

<span class="w">    </span><span class="n">c</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The place that is captured for <code>t1.0.0</code> would be <code>base: t1, projections: [{ Field(0), &amp;mut (i32, i32) {Deref, (i32, i32)}, {Field(0), i32}]</code></p>



<a name="218355707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218355707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218355707">(Nov 30 2020 at 22:29)</a>:</h4>
<p>I would expect to just always use <code>mut self</code></p>



<a name="218355745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218355745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218355745">(Nov 30 2020 at 22:29)</a>:</h4>
<p>Also if I look <a href="https://github.com/rust-lang/rust/blob/c4fe25d8613a32dcd71e5258c22ff5685f12d4c3/compiler/rustc_mir/src/borrow_check/mod.rs#L157-L178">here</a> I don't see this looking for &amp;mut when setting mutability here</p>



<a name="218355754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218355754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218355754">(Nov 30 2020 at 22:29)</a>:</h4>
<p>here, are you assuming a <code>move</code> closure or not?</p>



<a name="218355871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218355871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218355871">(Nov 30 2020 at 22:30)</a>:</h4>
<p>that said, in that example above, I still don't <em>quite</em> get the problem, wouldn't we be storing an <code>&amp;mut i32</code>?</p>



<a name="218355961"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218355961" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218355961">(Nov 30 2020 at 22:31)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/mutability.20of.20captured.20path/near/218355745">said</a>:</p>
<blockquote>
<p>Also if I look <a href="https://github.com/rust-lang/rust/blob/c4fe25d8613a32dcd71e5258c22ff5685f12d4c3/compiler/rustc_mir/src/borrow_check/mod.rs#L157-L178">here</a> I don't see this looking for &amp;mut when setting mutability here</p>
</blockquote>
<p>oh hmm</p>



<a name="218355962"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218355962" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218355962">(Nov 30 2020 at 22:31)</a>:</h4>
<p>The type is <code>i32</code> as far as I remember, lemme double check</p>



<a name="218355965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218355965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218355965">(Nov 30 2020 at 22:31)</a>:</h4>
<p>I'm remembering something now :)</p>



<a name="218355988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218355988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218355988">(Nov 30 2020 at 22:31)</a>:</h4>
<p>Also, this might be incorrectly done: <a href="https://github.com/rust-lang/rust/blob/977aa99592c7f8449f591e05d552c0f93afc1652/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs#L387-L428">https://github.com/rust-lang/rust/blob/977aa99592c7f8449f591e05d552c0f93afc1652/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs#L387-L428</a></p>



<a name="218355991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218355991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218355991">(Nov 30 2020 at 22:31)</a>:</h4>
<p>I had forgotten that we kind of special-cased closure upvars in the mutability check</p>



<a name="218356066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356066">(Nov 30 2020 at 22:32)</a>:</h4>
<p>that's annoying</p>



<a name="218356077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356077">(Nov 30 2020 at 22:32)</a>:</h4>
<p>but ok I think I see your point better</p>



<a name="218356117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356117">(Nov 30 2020 at 22:32)</a>:</h4>
<p><em>right</em> so today we decide that an upvar is mutable based on whether the variable is mutable</p>



<a name="218356165"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356165" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356165">(Nov 30 2020 at 22:33)</a>:</h4>
<p>sigh.</p>



<a name="218356190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356190">(Nov 30 2020 at 22:33)</a>:</h4>
<p>some part of me wonders if that logic belong in borrow check</p>



<a name="218356198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356198">(Nov 30 2020 at 22:33)</a>:</h4>
<p>but let's not worry about that for now :P</p>



<a name="218356215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356215">(Nov 30 2020 at 22:33)</a>:</h4>
<p>ok, I guess you have to store some extra information indeed</p>



<a name="218356305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356305">(Nov 30 2020 at 22:34)</a>:</h4>
<p>or at least...</p>



<a name="218356324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356324">(Nov 30 2020 at 22:34)</a>:</h4>
<p>is <code>closure_captures</code> the new or old style table?</p>



<a name="218356335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356335">(Nov 30 2020 at 22:34)</a>:</h4>
<p>that is old style</p>



<a name="218356377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356377">(Nov 30 2020 at 22:35)</a>:</h4>
<p>Ported code with new info is here: <a href="https://github.com/rust-lang/rust/pull/79553/commits/08fb1ec5c248abc77e733387b1181a5bfb1a2acf">https://github.com/rust-lang/rust/pull/79553/commits/08fb1ec5c248abc77e733387b1181a5bfb1a2acf</a></p>



<a name="218356400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356400">(Nov 30 2020 at 22:35)</a>:</h4>
<p>OK</p>



<a name="218356408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356408">(Nov 30 2020 at 22:35)</a>:</h4>
<p>that's what I figured</p>



<a name="218356576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356576">(Nov 30 2020 at 22:36)</a>:</h4>
<p>so I guess the logic might be something like: if it is captured by mut ref, it needs to be mut; if it is captured by <em>value</em>, then maybe we want to track "mutable value"</p>



<a name="218356695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356695">(Nov 30 2020 at 22:38)</a>:</h4>
<p>That could work</p>



<a name="218356805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356805">(Nov 30 2020 at 22:39)</a>:</h4>
<p>How do we assure that what we are assuming is correct, i,e, if we are borrowing with mut borrow it is mutable. I'm assuming that's just typeck</p>



<a name="218356910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356910">(Nov 30 2020 at 22:40)</a>:</h4>
<p>The reason I ask is because, borrow check also has the mutability errors.</p>



<a name="218356918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356918">(Nov 30 2020 at 22:40)</a>:</h4>
<p>well</p>



<a name="218356922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356922">(Nov 30 2020 at 22:40)</a>:</h4>
<p>if we do a mut borrow</p>



<a name="218356927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356927">(Nov 30 2020 at 22:41)</a>:</h4>
<p>and the path is not mutable</p>



<a name="218356936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356936">(Nov 30 2020 at 22:41)</a>:</h4>
<p>then the closure creator will get an error</p>



<a name="218356949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356949">(Nov 30 2020 at 22:41)</a>:</h4>
<p>but it may be a bad user experience, it's true</p>



<a name="218356967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356967">(Nov 30 2020 at 22:41)</a>:</h4>
<p>we could also have typeck compute whether upvar path is mutable</p>



<a name="218356974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356974">(Nov 30 2020 at 22:41)</a>:</h4>
<p>and store that separately</p>



<a name="218356979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356979">(Nov 30 2020 at 22:41)</a>:</h4>
<p>it's not super complicated</p>



<a name="218356987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218356987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218356987">(Nov 30 2020 at 22:41)</a>:</h4>
<p>that could work</p>



<a name="218357057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357057">(Nov 30 2020 at 22:42)</a>:</h4>
<p>I'm assuming check if the place_base or any of the projections has a TypeMut or something along those lines?</p>



<a name="218357076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357076">(Nov 30 2020 at 22:42)</a>:</h4>
<p>yeah you'd have walk down the projections</p>



<a name="218357084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357084">(Nov 30 2020 at 22:42)</a>:</h4>
<p>if the base is mutable, it starts as mutable</p>



<a name="218357089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357089">(Nov 30 2020 at 22:42)</a>:</h4>
<p>if you deref an <code>&amp;mut</code>, it becomes mutable</p>



<a name="218357094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357094">(Nov 30 2020 at 22:42)</a>:</h4>
<p>if you deref an <code>&amp;</code>, it is not</p>



<a name="218357100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357100">(Nov 30 2020 at 22:42)</a>:</h4>
<p>something like that</p>



<a name="218357120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357120">(Nov 30 2020 at 22:42)</a>:</h4>
<p>I sort of like the idea of storing it separately</p>



<a name="218357138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357138">(Nov 30 2020 at 22:43)</a>:</h4>
<p>then if there's a mismatch I guess it results in a type error</p>



<a name="218357150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357150">(Nov 30 2020 at 22:43)</a>:</h4>
<p>feels a bit less "trusting"</p>



<a name="218357180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357180">(Nov 30 2020 at 22:43)</a>:</h4>
<p>Might have some edge cases</p>
<div class="codehilite"><pre><span></span><code>let x = 10;
let mut refx = &amp;x;

let || {
    *refx += 10;
}
</code></pre></div>



<a name="218357197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357197">(Nov 30 2020 at 22:43)</a>:</h4>
<p><code>refx</code> is mut but the place being captured isn't</p>



<a name="218357221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357221">(Nov 30 2020 at 22:44)</a>:</h4>
<p>yeah that would be covered by what I wrote</p>



<a name="218357248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357248">(Nov 30 2020 at 22:44)</a>:</h4>
<p>because it's a deref of the <code>&amp;T</code></p>



<a name="218357271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357271">(Nov 30 2020 at 22:44)</a>:</h4>
<p>I guess <code>if the base is mutable, it starts as mutable</code> was a bit conflicting</p>



<a name="218357280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357280">(Nov 30 2020 at 22:44)</a>:</h4>
<p>but yea</p>



<a name="218357319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357319">(Nov 30 2020 at 22:45)</a>:</h4>
<p>Also maybe dropping any trailing derefs is also just a decent thing to do, makes mir build also a little faster because smaller projection list</p>



<a name="218357329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357329">(Nov 30 2020 at 22:45)</a>:</h4>
<p>and doesn't affect any precise capture</p>



<a name="218357411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357411">(Nov 30 2020 at 22:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/mutability.20of.20captured.20path/near/218357271">said</a>:</p>
<blockquote>
<p>I guess <code>if the base is mutable, it starts as mutable</code> was a bit conflicting</p>
</blockquote>
<p>I meant that the mutability changes as you walk down the projections and you get the mutability from the final point</p>



<a name="218357425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357425">(Nov 30 2020 at 22:46)</a>:</h4>
<p>Oh I see</p>



<a name="218357430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357430">(Nov 30 2020 at 22:46)</a>:</h4>
<p>or you could do it by walking <em>up</em> the projections and stopping when you reach a deref of an <code>&amp;T</code> (or take mutability of base if you never do)</p>



<a name="218357433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357433">(Nov 30 2020 at 22:46)</a>:</h4>
<p>something like that</p>



<a name="218357461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357461">(Nov 30 2020 at 22:46)</a>:</h4>
<p>anyway it seems like we will need something like this</p>



<a name="218357468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218357468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218357468">(Nov 30 2020 at 22:46)</a>:</h4>
<p>yeah</p>



<a name="218629037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218629037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218629037">(Dec 02 2020 at 22:26)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  any idea what might be triggerring this?</p>
<p><a href="/user_uploads/4715/8Bsz3WN0vf-Ao1tpDFPlIMUA/image.png">image.png</a> </p>
<div class="message_inline_image"><a href="/user_uploads/4715/8Bsz3WN0vf-Ao1tpDFPlIMUA/image.png" title="image.png"><img src="/user_uploads/4715/8Bsz3WN0vf-Ao1tpDFPlIMUA/image.png"></a></div><p>Happens when I use the feature by default to compile stdlib</p>



<a name="218642846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218642846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218642846">(Dec 03 2020 at 01:13)</a>:</h4>
<p>more of the code<br>
<a href="/user_uploads/4715/QG44fdYZr5QPNRrYdf-Eqzcl/image.png">image.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/QG44fdYZr5QPNRrYdf-Eqzcl/image.png" title="image.png"><img src="/user_uploads/4715/QG44fdYZr5QPNRrYdf-Eqzcl/image.png"></a></div>



<a name="218706184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706184">(Dec 03 2020 at 15:12)</a>:</h4>
<p>yeah I have some idea :)</p>



<a name="218706192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706192">(Dec 03 2020 at 15:12)</a>:</h4>
<p>heh, this is interesting!</p>



<a name="218706215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706215">(Dec 03 2020 at 15:12)</a>:</h4>
<p>I think we cannot capture through derefs of raw pointers</p>



<a name="218706233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706233">(Dec 03 2020 at 15:13)</a>:</h4>
<p>so we must be capturing <code>*leaf_ptr</code></p>



<a name="218706244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706244">(Dec 03 2020 at 15:13)</a>:</h4>
<p>which is unsafe to capture</p>



<a name="218706261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706261">(Dec 03 2020 at 15:13)</a>:</h4>
<p>i.e., <code>leaf_ptr: *mut T</code> or something</p>



<a name="218706274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706274">(Dec 03 2020 at 15:13)</a>:</h4>
<p>so that requires an unsafe block</p>



<a name="218706292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706292">(Dec 03 2020 at 15:13)</a>:</h4>
<p>it is indeed invalid for us to move when that deref occurs</p>



<a name="218706302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706302">(Dec 03 2020 at 15:13)</a>:</h4>
<p><span class="user-mention" data-user-id="281950">@Aman Arora</span> <span aria-label="point up" class="emoji emoji-1f446" role="img" title="point up">:point_up:</span></p>



<a name="218706335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706335">(Dec 03 2020 at 15:13)</a>:</h4>
<p>we can only capture a deref place that derefs through an <code>&amp;T</code>, <code>&amp;mut T</code> or <code>Box&lt;T&gt;</code>, I think</p>



<a name="218706366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706366">(Dec 03 2020 at 15:13)</a>:</h4>
<p>not <code>*mut T</code></p>



<a name="218706391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706391">(Dec 03 2020 at 15:14)</a>:</h4>
<p>(I think we already don't capture overloaded derefs, right)</p>



<a name="218706489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/218706489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#218706489">(Dec 03 2020 at 15:14)</a>:</h4>
<p>Okay that makes sense based on some of the logic that I have seen around</p>



<a name="219927663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219927663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219927663">(Dec 15 2020 at 01:31)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  is it correct to say that in case of move by value, the captured path is always mutable. </p>
<p>My reasoning behind this is that move kind of creates sort of a new binding within the closure structure and therefore that binding can now be mutable. If we do end up doing the reborrowing approach it will still work because it just suggests that what we are borrow can change and not the contents of  what is being borrowed</p>



<a name="219928004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219928004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219928004">(Dec 15 2020 at 01:37)</a>:</h4>
<p>The reason I ask this is because, today when you run the following code, it suggests that t must be <code>mut</code> regardless of if our feature is enabled or not.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">no_ref</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">struct</span> <span class="nc">S</span><span class="p">(</span><span class="nb">String</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">struct</span> <span class="nc">T</span><span class="p">(</span><span class="n">S</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w">  </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="s">"s"</span><span class="p">.</span><span class="n">into</span><span class="p">()));</span><span class="w"> </span><span class="c1">//~ ERROR: t must be mut</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">t</span><span class="p">.</span><span class="mf">0.0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"new S"</span><span class="p">.</span><span class="n">into</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="n">c</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>When the feature is enabled and I run with <code>let mut t</code> I get a warning that t doesn't need to be mut and it makes sense because  the following is valid code</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">S</span><span class="p">(</span><span class="nb">String</span><span class="p">);</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">T</span><span class="p">(</span><span class="n">S</span><span class="p">);</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">no_ref</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">s</span>: <span class="nb">String</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">s</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">format</span><span class="o">!</span><span class="p">(</span><span class="s">"s"</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">s</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="s">"old"</span><span class="p">.</span><span class="n">into</span><span class="p">()));</span><span class="w"> </span><span class="c1">//~ ERROR: t must be mut</span>
<span class="w">    </span><span class="n">no_ref</span><span class="p">(</span><span class="n">t</span><span class="p">.</span><span class="mf">0.0</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="219961252"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961252" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961252">(Dec 15 2020 at 10:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/mutability.20of.20captured.20path/near/219927663">said</a>:</p>
<blockquote>
<p>is it correct to say that in case of move by value, the captured path is always mutable. </p>
</blockquote>
<p>No, it's not correct. Based on the <strong>desuaring</strong>, it would stand to reason, but we have rather special treatment for captured variables in closures that attempts to make captured things mutable only if they would have been mutable in the creating function (I was confused about this earlier)</p>



<a name="219961359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961359">(Dec 15 2020 at 10:44)</a>:</h4>
<p>We talked about this some and came up with some rules for mutability, <span class="user-mention" data-user-id="281950">@Aman Arora</span>, but I'll have to go back to find the precise comment in the history</p>



<a name="219961443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961443">(Dec 15 2020 at 10:45)</a>:</h4>
<p>yeah this wasn't one of what we had discussed, it came up because of this example</p>



<a name="219961481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961481">(Dec 15 2020 at 10:46)</a>:</h4>
<p>what we had discussed was if we deref a immut-borrow we will have an immutable place</p>



<a name="219961613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961613">(Dec 15 2020 at 10:47)</a>:</h4>
<p>well</p>



<a name="219961622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961622">(Dec 15 2020 at 10:47)</a>:</h4>
<p>I think the same rule that I outlined there applies here</p>



<a name="219961658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961658">(Dec 15 2020 at 10:47)</a>:</h4>
<p>basically that we want to derive the mutability of a given upvar by looking at the mutability of the place of which it is a capture</p>



<a name="219961726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961726">(Dec 15 2020 at 10:48)</a>:</h4>
<p>I guess my first question is, what do we expect here</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">no_ref</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">struct</span> <span class="nc">S</span><span class="p">(</span><span class="nb">String</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">struct</span> <span class="nc">T</span><span class="p">(</span><span class="n">S</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w">  </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="s">"s"</span><span class="p">.</span><span class="n">into</span><span class="p">()));</span><span class="w"> </span><span class="c1">//~ ERROR: t must be mut</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">t</span><span class="p">.</span><span class="mf">0.0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"new S"</span><span class="p">.</span><span class="n">into</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="n">c</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="219961740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961740">(Dec 15 2020 at 10:48)</a>:</h4>
<p>should t be mutable for this to work with our feature enabled</p>



<a name="219961833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961833">(Dec 15 2020 at 10:49)</a>:</h4>
<p>yes</p>



<a name="219961849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961849">(Dec 15 2020 at 10:49)</a>:</h4>
<p>I think the error is correct</p>



<a name="219961925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961925">(Dec 15 2020 at 10:50)</a>:</h4>
<p>note that you get the same error today</p>



<a name="219961942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961942">(Dec 15 2020 at 10:50)</a>:</h4>
<p>yup</p>



<a name="219961953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961953">(Dec 15 2020 at 10:50)</a>:</h4>
<p>basically the situation is:</p>



<a name="219961962"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961962" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961962">(Dec 15 2020 at 10:50)</a>:</h4>
<ul>
<li>you are correct that if you desugar, mut ought not to be required</li>
</ul>



<a name="219961975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961975">(Dec 15 2020 at 10:50)</a>:</h4>
<ul>
<li>but for end users, they are not thinking of the desugaring</li>
</ul>



<a name="219961988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961988">(Dec 15 2020 at 10:51)</a>:</h4>
<p>Fair point</p>



<a name="219961995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219961995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219961995">(Dec 15 2020 at 10:51)</a>:</h4>
<p>it is a bit weird because it means there isn't really a desugaring you can do</p>



<a name="219962007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962007">(Dec 15 2020 at 10:51)</a>:</h4>
<p>because our mutability system isn't sufficiently expressive</p>



<a name="219962016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962016">(Dec 15 2020 at 10:51)</a>:</h4>
<p>but...oh well</p>



<a name="219962027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962027">(Dec 15 2020 at 10:51)</a>:</h4>
<p>I'd rather fix that by expanding the mutability system some day, if we think it's important</p>



<a name="219962030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962030">(Dec 15 2020 at 10:51)</a>:</h4>
<p>I guess I need to work on suppressing the warning</p>



<a name="219962094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962094">(Dec 15 2020 at 10:52)</a>:</h4>
<p>Because if I enable capture_disjoint_fields and use <code>mut t</code>, compiler tells me t doesn't need to be mut. Which also makes sense</p>



<a name="219962095"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962095" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962095">(Dec 15 2020 at 10:52)</a>:</h4>
<blockquote>
<p>it is a bit weird because it means there isn't really a desugaring you can do</p>
</blockquote>
<p>right, and it's precisely because of <span aria-label="point up" class="emoji emoji-1f446" role="img" title="point up">:point_up:</span> that the code can't be cleaner :(</p>



<a name="219962116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962116">(Dec 15 2020 at 10:52)</a>:</h4>
<p>(I guess arguably we could implement a more expressive mutability system internally to the compiler)</p>



<a name="219962138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962138">(Dec 15 2020 at 10:52)</a>:</h4>
<p>what do you mean here</p>



<a name="219962158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962158">(Dec 15 2020 at 10:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/mutability.20of.20captured.20path/near/219962030">said</a>:</p>
<blockquote>
<p>I guess I need to work on suppressing the warning</p>
</blockquote>
<p>tbh I don't remember the details of how upvar mutability is handled in borrowck</p>



<a name="219962180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962180">(Dec 15 2020 at 10:53)</a>:</h4>
<p>this doesn't need to do with upvar mutability</p>



<a name="219962187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962187">(Dec 15 2020 at 10:53)</a>:</h4>
<p>so we have a local decl for t</p>



<a name="219962195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962195">(Dec 15 2020 at 10:53)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/mutability.20of.20captured.20path/near/219962138">said</a>:</p>
<blockquote>
<p>what do you mean here</p>
</blockquote>
<p>what I mean is that if you have <code>self.foo</code>, in today's Rust, you can either make <code>mut self</code> (and hence <em>all</em> fields are mutable) or <code>self</code> (no fields are mutable)</p>



<a name="219962207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962207">(Dec 15 2020 at 10:53)</a>:</h4>
<p>but you could imagine a mutability system where, for example, each field <em>also</em> had to be declared <code>mut</code></p>



<a name="219962211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962211">(Dec 15 2020 at 10:53)</a>:</h4>
<p>Oh yes</p>



<a name="219962215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962215">(Dec 15 2020 at 10:53)</a>:</h4>
<p>(in fact, I think we should've done this)</p>



<a name="219962226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962226">(Dec 15 2020 at 10:53)</a>:</h4>
<p>so e.g. <code>struct Foo { mut bar: u32, baz: u32 }</code></p>



<a name="219962283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962283">(Dec 15 2020 at 10:54)</a>:</h4>
<p>in that case, <code>self.bar = 22</code> would work iff <code>mut self</code>, but <code>self.baz = 44</code> would not</p>



<a name="219962303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962303">(Dec 15 2020 at 10:54)</a>:</h4>
<p>yea that makes sense</p>



<a name="219962316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962316">(Dec 15 2020 at 10:54)</a>:</h4>
<p>in any case I am not (currently) trying to argue for this as a good system for end-user Rust</p>



<a name="219962333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962333">(Dec 15 2020 at 10:54)</a>:</h4>
<p>but I am saying that the compiler could <em>internally</em> act "as if" this was how Rust worked</p>



<a name="219962336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962336">(Dec 15 2020 at 10:54)</a>:</h4>
<p>and then we could desugar closures into that</p>



<a name="219962354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962354">(Dec 15 2020 at 10:54)</a>:</h4>
<p>instead of having "Special case logic" for upvars</p>



<a name="219962373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962373">(Dec 15 2020 at 10:55)</a>:</h4>
<p>I suspect this is not the right thing for you to do right now</p>



<a name="219962385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962385">(Dec 15 2020 at 10:55)</a>:</h4>
<p>but it might be the cleaner approach in the end</p>



<a name="219962394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962394">(Dec 15 2020 at 10:55)</a>:</h4>
<p>seems more like a "after this is done" kind of refactoring to me though</p>



<a name="219962395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962395">(Dec 15 2020 at 10:55)</a>:</h4>
<p>Yeah</p>



<a name="219962441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962441">(Dec 15 2020 at 10:56)</a>:</h4>
<p>So the problem we have is because we have two local declarations, one is for t and the other is for <code>t.0</code> when the closure is constructed. We see <code>t</code> not being mutated and that's the issue.  I think special casing closures here might add a bit too much</p>



<a name="219962512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962512">(Dec 15 2020 at 10:56)</a>:</h4>
<p>oh</p>



<a name="219962520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962520">(Dec 15 2020 at 10:56)</a>:</h4>
<p>hmm</p>



<a name="219962527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962527">(Dec 15 2020 at 10:56)</a>:</h4>
<p>is this in the <em>liveness</em> code?</p>



<a name="219962534"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962534" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962534">(Dec 15 2020 at 10:56)</a>:</h4>
<p>or the <em>borrow check</em> code?</p>



<a name="219962547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962547">(Dec 15 2020 at 10:56)</a>:</h4>
<p>...where does that warning come from...</p>



<a name="219962551"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962551" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962551">(Dec 15 2020 at 10:56)</a>:</h4>
<p>this is in the borrow checker code</p>



<a name="219962586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962586">(Dec 15 2020 at 10:57)</a>:</h4>
<p><a href="https://github.com/sexxi-goose/rust/blob/master/compiler/rustc_mir/src/borrow_check/mod.rs#L437">https://github.com/sexxi-goose/rust/blob/master/compiler/rustc_mir/src/borrow_check/mod.rs#L437</a><br>
here</p>



<a name="219962613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962613">(Dec 15 2020 at 10:57)</a>:</h4>
<p>yeah so we really need a concept of 'mut by value' capture I guess</p>



<a name="219962637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962637">(Dec 15 2020 at 10:58)</a>:</h4>
<p>(also, I'll note that my desugaring idea above doesn't capture <em>this</em> aspect of things)</p>



<a name="219962678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962678">(Dec 15 2020 at 10:58)</a>:</h4>
<p>Well kind of</p>



<a name="219962717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962717">(Dec 15 2020 at 10:58)</a>:</h4>
<p>in particular it could make an error occur if you are missing a <code>mut</code></p>



<a name="219962742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962742">(Dec 15 2020 at 10:58)</a>:</h4>
<p>but for the lint, we need info to flow in the opposite direction, <em>from</em> the closure into the creator, indicating that the closure will require <code>mut</code></p>



<a name="219962771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962771">(Dec 15 2020 at 10:59)</a>:</h4>
<p>Yes</p>



<a name="219962873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962873">(Dec 15 2020 at 11:00)</a>:</h4>
<p>there must already be some special casing of closures happening here</p>



<a name="219962894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962894">(Dec 15 2020 at 11:00)</a>:</h4>
<p>presumably related to this <code>used_mut_upvars</code> field</p>



<a name="219962912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962912">(Dec 15 2020 at 11:00)</a>:</h4>
<p>yes, so <a href="https://github.com/sexxi-goose/rust/blob/master/compiler/rustc_mir/src/borrow_check/mod.rs#L1358">this function call</a></p>



<a name="219962923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962923">(Dec 15 2020 at 11:00)</a>:</h4>
<p><a href="https://github.com/sexxi-goose/rust/blob/e261649593cf9c2707f7b30a61c46c4469c67ebb/compiler/rustc_mir/src/borrow_check/mod.rs#L1373">this function</a></p>



<a name="219962982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962982">(Dec 15 2020 at 11:01)</a>:</h4>
<p>reading</p>



<a name="219962984"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219962984" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219962984">(Dec 15 2020 at 11:01)</a>:</h4>
<p>did you update that function at all?</p>



<a name="219963103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963103">(Dec 15 2020 at 11:02)</a>:</h4>
<p>pre-existing:</p>



<a name="219963112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963112">(Dec 15 2020 at 11:02)</a>:</h4>
<p><a href="https://github.com/sexxi-goose/rust/blob/e261649593cf9c2707f7b30a61c46c4469c67ebb/compiler/rustc_mir/src/borrow_check/mod.rs#L536-L538">this comment is woefully insufficient</a></p>



<a name="219963148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963148">(Dec 15 2020 at 11:02)</a>:</h4>
<p>oh, maybe not</p>



<a name="219963185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963185">(Dec 15 2020 at 11:03)</a>:</h4>
<p>I just updated the part where we set if a capture is mutable or not</p>



<a name="219963209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963209">(Dec 15 2020 at 11:03)</a>:</h4>
<p>I there a relationship b/w the local_decl entry of <code>t</code> vs <code>t.0</code>?</p>



<a name="219963213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963213">(Dec 15 2020 at 11:03)</a>:</h4>
<p>ok so .. <code>used_mut_upvars</code> is a list of field indices</p>



<a name="219963332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963332">(Dec 15 2020 at 11:04)</a>:</h4>
<p>I guess that can be used to extract the parent variable</p>



<a name="219963343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963343">(Dec 15 2020 at 11:04)</a>:</h4>
<p>I think that <a href="https://github.com/sexxi-goose/rust/blob/e261649593cf9c2707f7b30a61c46c4469c67ebb/compiler/rustc_mir/src/borrow_check/mod.rs#L1374">this logic is wrong</a></p>



<a name="219963437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963437">(Dec 15 2020 at 11:05)</a>:</h4>
<p>it probably needs to recurse</p>



<a name="219963442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963442">(Dec 15 2020 at 11:05)</a>:</h4>
<p>this might be a nested closure case</p>



<a name="219963453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963453">(Dec 15 2020 at 11:05)</a>:</h4>
<p>well so it's a bit tricky</p>



<a name="219963498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963498">(Dec 15 2020 at 11:05)</a>:</h4>
<p>right now though it does nothing if you </p>
<ul>
<li>have a projection</li>
<li>are not a upvar field projection</li>
</ul>



<a name="219963616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963616">(Dec 15 2020 at 11:07)</a>:</h4>
<p>I think what you want to do is to "pop" off the right-most projection until you either</p>
<ul>
<li>are just a local variable (in which case you add it to <code>used_muts</code>)</li>
<li>are a deref of a (shared or mutable) reference (in which case you do nothing)</li>
<li>or are an upvar field projection (in which case you record it as a 'used mut upvar')</li>
</ul>



<a name="219963665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963665">(Dec 15 2020 at 11:07)</a>:</h4>
<p>the middle case is kind of interesting, I'm remembering this corner case ... maybe it no longer applies in the MIR borrow check era</p>



<a name="219963771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963771">(Dec 15 2020 at 11:08)</a>:</h4>
<p>I think it sort of makes sense</p>



<a name="219963796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963796">(Dec 15 2020 at 11:09)</a>:</h4>
<p>would it not be easy to start searching from the start?</p>



<a name="219963817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963817">(Dec 15 2020 at 11:09)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">22</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</code></pre></div>
<p>(this requires <code>x</code> be captured by "unique" reference, which also isn't a concept that Rust exposes to end users)</p>



<a name="219963856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963856">(Dec 15 2020 at 11:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/mutability.20of.20captured.20path/near/219963796">said</a>:</p>
<blockquote>
<p>would it not be easy to start searching from the start?</p>
</blockquote>
<p>you could also do that, the reason I expessed it as going from the right-hand side is that you want to take action based on the "last" thing you find</p>



<a name="219963909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219963909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219963909">(Dec 15 2020 at 11:10)</a>:</h4>
<p>I would use the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Place.html#method.as_ref"><code>Place::as_ref</code></a> to convert to a slice</p>



<a name="219964006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964006">(Dec 15 2020 at 11:11)</a>:</h4>
<p>well, convert to a <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.PlaceRef.html"><code>PlaceRef</code></a></p>



<a name="219964030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964030">(Dec 15 2020 at 11:11)</a>:</h4>
<p>though <code>PlaceRef</code> seems to be missing the method I think would be most useful</p>



<a name="219964049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964049">(Dec 15 2020 at 11:11)</a>:</h4>
<p>hmm actually <span class="user-mention" data-user-id="120791">@RalfJ</span> recently added some helper for this in one of their PRs</p>



<a name="219964135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964135">(Dec 15 2020 at 11:12)</a>:</h4>
<p>I guess it didn't land yet</p>



<a name="219964137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964137">(Dec 15 2020 at 11:12)</a>:</h4>
<p>I don't quite think this solves the problem:</p>
<ol>
<li>are just a local variable (in which case you add it to used_muts). This is local variable within the closure, right?</li>
<li>are a deref of a (shared or mutable) reference (in which case you do nothing).</li>
<li>or are an upvar field projection (in which case you record it as a 'used mut upvar'). The Field projection we get here, is of the precise place</li>
</ol>



<a name="219964145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964145">(Dec 15 2020 at 11:12)</a>:</h4>
<p>because it's waiting on me to r+..d'oh</p>



<a name="219964191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964191">(Dec 15 2020 at 11:12)</a>:</h4>
<p>(PR <a href="https://github.com/rust-lang/rust/pull/78068">https://github.com/rust-lang/rust/pull/78068</a>)</p>



<a name="219964240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964240">(Dec 15 2020 at 11:13)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/mutability.20of.20captured.20path/near/219964137">said</a>:</p>
<blockquote>
<ol>
<li>are just a local variable (in which case you add it to used_muts). This is local variable within the closure, right?</li>
</ol>
</blockquote>
<p>no, this will be the local variable in the closure creator</p>
<blockquote>
<ol start="3">
<li>or are an upvar field projection (in which case you record it as a 'used mut upvar'). The Field projection we get here, is of the precise place</li>
</ol>
</blockquote>
<p>not sure I follow this</p>



<a name="219964250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964250">(Dec 15 2020 at 11:13)</a>:</h4>
<p>in particular, the <code>Place</code> we are iterating here is the <em>place that was captured</em></p>



<a name="219964260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964260">(Dec 15 2020 at 11:13)</a>:</h4>
<p>i.e., from the perspective of the closure <em>creator</em></p>



<a name="219964376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964376">(Dec 15 2020 at 11:14)</a>:</h4>
<p>Okay, yeah I was confused a bit about that. Okay makes more sense. How do we differentiate a Field from an upvar field projection?</p>



<a name="219964415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964415">(Dec 15 2020 at 11:15)</a>:</h4>
<p>also just to confirm that would be a nested closure case?</p>



<a name="219964429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964429">(Dec 15 2020 at 11:15)</a>:</h4>
<p>the <a href="https://github.com/sexxi-goose/rust/blob/e261649593cf9c2707f7b30a61c46c4469c67ebb/compiler/rustc_mir/src/borrow_check/mod.rs#L1376"><code>is_upvar_field_projection</code></a> function can do that for you</p>



<a name="219964440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964440">(Dec 15 2020 at 11:15)</a>:</h4>
<p>but basically the answer is: if you're in a closure and you have <code>self.foo</code></p>



<a name="219964444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964444">(Dec 15 2020 at 11:15)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/mutability.20of.20captured.20path/near/219964415">said</a>:</p>
<blockquote>
<p>also just to confirm that would be a nested closure case?</p>
</blockquote>
<p>yes</p>



<a name="219964456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964456">(Dec 15 2020 at 11:15)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/mutability.20of.20captured.20path/near/219964429">said</a>:</p>
<blockquote>
<p>the <a href="https://github.com/sexxi-goose/rust/blob/e261649593cf9c2707f7b30a61c46c4469c67ebb/compiler/rustc_mir/src/borrow_check/mod.rs#L1376"><code>is_upvar_field_projection</code></a> function can do that for you</p>
</blockquote>
<p>oh right <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="219964472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964472">(Dec 15 2020 at 11:15)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/mutability.20of.20captured.20path/near/219964440">said</a>:</p>
<blockquote>
<p>but basically the answer is: if you're in a closure and you have <code>self.foo</code></p>
</blockquote>
<p>or <code>*self.foo</code></p>



<a name="219964627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964627">(Dec 15 2020 at 11:17)</a>:</h4>
<p>I'll try this out later today, and see how this goes</p>



<a name="219964953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/219964953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#219964953">(Dec 15 2020 at 11:20)</a>:</h4>
<p>Great!</p>



<a name="220170913"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/220170913" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#220170913">(Dec 16 2020 at 20:48)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  I created a PR for this: <a href="https://github.com/rust-lang/rust/pull/80092">https://github.com/rust-lang/rust/pull/80092</a> </p>
<p>There was another unexpected warning, I was able to hack around it, but I'm wasn't able to find/fix the root cause <span aria-label="confused" class="emoji emoji-1f615" role="img" title="confused">:confused:</span> </p>
<p>I left the details in the PR.</p>



<a name="220228937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/220228937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#220228937">(Dec 17 2020 at 10:53)</a>:</h4>
<p><span class="user-mention" data-user-id="281950">@Aman Arora</span> left some comments -- I want to double check that I am understanding what you are referring to about the unexpected warning. Maybe I missed it.</p>



<a name="220334434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/220334434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#220334434">(Dec 18 2020 at 03:51)</a>:</h4>
<p>You were right, handling the not deref didn't fix the issue</p>



<a name="220385713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/220385713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#220385713">(Dec 18 2020 at 15:34)</a>:</h4>
<p><span class="user-mention" data-user-id="281950">@Aman Arora</span> I can try to dig a bit deeper perhaps, though my coding time is very limited :)</p>



<a name="220404792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/220404792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#220404792">(Dec 18 2020 at 17:44)</a>:</h4>
<p>Thank you <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="222205317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222205317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222205317">(Jan 10 2021 at 04:29)</a>:</h4>
<p>I'm going to do a brain dump here, in case something is useful:</p>
<ul>
<li>So one thing that I observed was that it is now possible that a path itself is a capture</li>
<li>This wasn't previously the case , which means (previously) if capture wasn't mutable that was because it wasn't declared mutable</li>
</ul>



<a name="222205486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222205486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222205486">(Jan 10 2021 at 04:34)</a>:</h4>
<p>Without the feature enabled is_mutable is called twice that stood out to me</p>
<div class="codehilite"><pre><span></span><code>-&gt; DEBUG rustc_mir::borrow_check is_mutable: place=PlaceRef { local: _1, projection: [Deref, Field(field[0], &amp;mut (&amp;mut (&amp;(std::string::String, std::string::String), &amp;str), &amp;str)), Deref, Field(field[0], &amp;mut (&amp;(std::string::String, std::string::String), &amp;str)), Deref, Field(field[0], &amp;(std::string::String, std::string::String)), Deref, Field(field[0], std::string::String)] }, is_local...=No

-&gt; DEBUG rustc_mir::borrow_check is_mutable: place=PlaceRef { local: _1, projection: [Deref, Field(field[0], &amp;mut (&amp;mut (&amp;(std::string::String, std::string::String), &amp;str), &amp;str)), Deref, Field(field[0], &amp;mut (&amp;(std::string::String, std::string::String), &amp;str)), Deref, Field(field[0], &amp;(std::string::String, std::string::String)), Deref] }, is_local...=No
</code></pre></div>
<p>With the feature enabled</p>
<div class="codehilite"><pre><span></span><code>-&gt;  DEBUG rustc_mir::borrow_check is_mutable: place=PlaceRef { local: _1, projection: [Deref, Field(field[0], &amp;mut std::string::String), Deref] }, is_local...=No

-&gt;  DEBUG rustc_mir::borrow_check is_mutable: place=PlaceRef { local: _1, projection: [Deref, Field(field[0], &amp;mut std::string::String)] }, is_local...=No

-&gt; DEBUG rustc_mir::borrow_check is_mutable: upvar.mutability=Upvar { name: &quot;z&quot;, place: CapturedPlace { place: Place { base_ty: (&amp;mut (&amp;(std::string::String, std::string::String), &amp;str), &amp;str), base: Upvar(UpvarId(HirId { owner: DefId(0:3 ~ cant_mutate_imm_borrow[317d]::main), local_id: 76 };`z`;DefId(0:4 ~ cant_mutate_imm_borrow[317d]::main::{closure#0}))), projections: [Projection { ty: &amp;mut (&amp;(std::string::String, std::string::String), &amp;str), kind: Field(0, 0) }, Projection { ty: (&amp;(std::string::String, std::string::String), &amp;str), kind: Deref }, Projection { ty: &amp;(std::string::String, std::string::String), kind: Field(0, 0) }, Projection { ty: (std::string::String, std::string::String), kind: Deref }, Projection { ty: std::string::String, kind: Field(0, 0) }] }, info: CaptureInfo { expr_id: Some(HirId { owner: DefId(0:3 ~ cant_mutate_imm_borrow[317d]::main), local_id: 82 }), capture_kind: ByRef(UpvarBorrow(UniqueImmBorrow, ReErased)) }, mutability: Not }, by_ref: true } local_mutation_is_allowed=No place=PlaceRef { local: _1, projection: [Deref, Field(field[0], &amp;mut std::string::String)] }, place_base=PlaceRef { local: _1, projection: [Deref] }

..... more is_mutable ....

-&gt; DEBUG rustc_mir::borrow_check is_mutable: place=PlaceRef { local: _28, projection: [Field(field[0], &amp;mut (&amp;(std::string::String, std::string::String), &amp;str)), Deref, Field(field[0], &amp;(std::string::String, std::string::String)), Deref, Field(field[0], std::string::String)] }, is_local...=Yes

-&gt; DEBUG rustc_mir::borrow_check is_mutable: place=PlaceRef { local: _28, projection: [Field(field[0], &amp;mut (&amp;(std::string::String, std::string::String), &amp;str)), Deref, Field(field[0], &amp;(std::string::String, std::string::String)), Deref] }, is_local...=Yes
</code></pre></div>



<a name="222323039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222323039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222323039">(Jan 11 2021 at 16:44)</a>:</h4>
<p>So I have some speculation for this issue:</p>
<ul>
<li>
<p>If we consider the case without the feature:</p>
<ul>
<li>We have a path starting off of a capture being used and not the capture being itself</li>
<li>This means we store this info in used_muts for some local thing based on this</li>
<li>this will fail because of &amp; borrow</li>
</ul>
</li>
<li>
<p>if the feature is enabled</p>
<ul>
<li>we see that we are trying to modify a capture, which has Mutability::Not.</li>
<li>this probably gets added  to used_mut_upvars </li>
<li>throws an error when we propagate the mut requirements up  and we think that the root variable needs to be mut</li>
</ul>
</li>
</ul>



<a name="222323113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222323113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222323113">(Jan 11 2021 at 16:45)</a>:</h4>
<p>well that was sitting in the draft, woops</p>



<a name="222323291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222323291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222323291">(Jan 11 2021 at 16:46)</a>:</h4>
<p>So I was looking at some more logs around this and this feels like the MIR might be wrong</p>



<a name="222323324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222323324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222323324">(Jan 11 2021 at 16:47)</a>:</h4>
<div class="codehilite"><pre><span></span><code>DEBUG rustc_mir::borrow_check check_access_permissions((*((*_1).0: &amp;mut std::string::String)), Write(Mutate), is_local_mutation_allowed: No)
DEBUG rustc_mir::borrow_check is_mutable: place=PlaceRef { local: _1, projection: [Deref, Field(field[0], &amp;mut std::string::String), Deref] }, is_local...=No
DEBUG rustc_mir::borrow_check is_mutable: place=PlaceRef { local: _1, projection: [Deref, Field(field[0], &amp;mut std::string::String)] }, is_local...=No
DEBUG rustc_mir::borrow_check is_mutable: upvar.mutability=Upvar { name: &quot;z&quot;, place: CapturedPlace { place: Place { base_ty: (&amp;mut (&amp;(std::string::String, std::string::String), &amp;str), &amp;str), base: Upvar(UpvarId(HirId { owner: DefId(0:3 ~ cant_mutate_imm_borrow[317d]::main), local_id: 76 };`z`;DefId(0:4 ~ cant_mutate_imm_borrow[317d]::main::{closure#0}))), projections: [Projection { ty: &amp;mut (&amp;(std::string::String, std::string::String), &amp;str), kind: Field(0, 0) }, Projection { ty: (&amp;(std::string::String, std::string::String), &amp;str), kind: Deref }, Projection { ty: &amp;(std::string::String, std::string::String), kind: Field(0, 0) }, Projection { ty: (std::string::String, std::string::String), kind: Deref }, Projection { ty: std::string::String, kind: Field(0, 0) }] }, info: CaptureInfo { expr_id: Some(HirId { owner: DefId(0:3 ~ cant_mutate_imm_borrow[317d]::main), local_id: 82 }), capture_kind: ByRef(UpvarBorrow(UniqueImmBorrow, ReErased)) }, mutability: Not }, by_ref: true } local_mutation_is_allowed=No place=PlaceRef { local: _1, projection: [Deref, Field(field[0], &amp;mut std::string::String)] }, place_base=PlaceRef { local: _1, projection: [Deref] }
DEBUG rustc_mir::borrow_check::diagnostics::mutability_errors report_mutability_error(access_place=(*((*_1).0: &amp;mut std::string::String)), span=cant-mutate-imm-borrow.rs:16:9: 16:16 (#0), the_place_err=PlaceRef { local: _1, projection: [Deref, Field(field[0], &amp;mut std::string::String)] }, error_access=Mutate, location=bb3[1],)
DEBUG rustc_mir::borrow_check::diagnostics::mutability_errors report_mutability_error: access_place_desc=Some(&quot;z&quot;)
</code></pre></div>



<a name="222324840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222324840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222324840">(Jan 11 2021 at 16:57)</a>:</h4>
<p>nvm I'm dumb we expect the capture type to be &amp;mut string, that's the typeck part we were discussing</p>



<a name="222365456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222365456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222365456">(Jan 11 2021 at 22:13)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/mutability.20of.20captured.20path/near/222323039">said</a>:</p>
<blockquote>
<ul>
<li>if the feature is enabled<ul>
<li>we see that we are trying to modify a capture, which has Mutability::Not.</li>
<li>this probably gets added  to used_mut_upvars </li>
<li>throws an error when we propagate the mut requirements up  and we think that the root variable needs to be mut</li>
</ul>
</li>
</ul>
</blockquote>
<p>So this is incorrect, we wouldn't add it to the used_upvar_muts because the is_mutable test fails for them</p>



<a name="222460509"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222460509" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222460509">(Jan 12 2021 at 16:54)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  Okay I think I got this. This isn't quite a bug</p>
<p>Well there is a logging bug i think, but not quite an actual bug</p>



<a name="222460971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222460971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222460971">(Jan 12 2021 at 16:56)</a>:</h4>
<p>if u look at this: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=d207754d7a09e7b11ff20d2ac0b746a2">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=d207754d7a09e7b11ff20d2ac0b746a2</a><br>
we see two logs being emitted. This example is closer to the test case with the feature enabled because a capture is directly being mutated.</p>
<p>We can see that we emit two errors here: One is thrown when we are borrow checking the closure because the capture isn't mutable and therefore can't be assigned to. The second is when we process the function, in which case the error is when we try to mut borrow so that we can create the closure</p>



<a name="222462895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222462895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222462895">(Jan 12 2021 at 17:08)</a>:</h4>
<p>Now the problem arises from the notion that if a capture is immutable then that must be because the root variable is immutable</p>



<a name="222463177"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/mutability%20of%20captured%20path/near/222463177" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/mutability.20of.20captured.20path.html#222463177">(Jan 12 2021 at 17:09)</a>:</h4>
<p>we could use this assumption to provide the suggestion to mark that place mut but that might not be a correct suggestion when a precise path is captured.</p>
<p>So my hack is not quite a hack <span aria-label="joy" class="emoji emoji-1f602" role="img" title="joy">:joy:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>